import grequests #封装了协程gevent库
urls = ['https://mercury.jd.com/log.gif']*11
import time
def print_url(r, *args, **kwargs):
    print(r.url)
    time.sleep(10)
rs = (grequests.get(u, callback=print_url) for u in urls)
print(grequests.map(rs,size=4))#
#########协程,遇到IO或者休眠线程会执行其他的操作
from gevent import monkey
monkey.patch_all()
import gevent,requests,bs4,openpyxl,time
from gevent.queue import Queue

work = Queue()
url='https://food.hiyd.com/list-132-html?page={}'
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36'}

[work.put_nowait(url.format(i)) for i in range(1,20)]
print(work)

tasks_list = []
def crawler(work,i):
      while not work.empty():
          item = work.get_nowait()
          gevent.sleep(3)
          print(i)
          print(requests.get(item,headers=headers).status_code)
# def crawler(url):
#     print(requests.get(url).status_code)
tasks_list = [gevent.spawn(crawler,work,i) for i in range(5)]
# tasks_list = [gevent.spawn(crawler,url.format(i)) for i in range(10)]
gevent.joinall(tasks_list) #一次启动5个协程
# work.get_nowait()
len(work)











###GIL锁 https://zhuanlan.zhihu.com/p/97218985